const {
	ElMessage
} = require("element-plus");
export default {
	name: "Food",
	data() {
		return {
			//回显文件流
			fromImg: '',
			imgUrl: 'http://localhost:8080/img/',
			//加载
			loading: true,
			pageNum: 1,
			pageSize: 5,
			total: 0,
			//小弹窗状态
			dialogVisible: false,
			//弹窗是否为更新状态
			judge: false,

			imgPath: '',
			tableData: [],
			searchPARAM: {
				id: '',
				name: '',
				type: '',
				content: '',
				restaurantName: '',
				restaurantAddress: '',
				img: ''
			},
			form: {
				id: '',
				name: '',
				type: '',
				content: '',
				restaurantName: '',
				restaurantAddress: '',
				img: '',
			},


		}
	},

	created() {

		this.load();
		this.loading = true;
		setTimeout(() => {
			//设置延迟执行
			this.loading = false;
		}, 500);
	},


	methods: {


		upload(files) {
		
			
			this.fromImg = window.URL.createObjectURL(files.file);
			const fd = new FormData();
			fd.append("imgFile",files.file);
			this.$api.post("/file/img/upload",fd, {headers: {"Content-Type": "multipart/form-data"}})
				.then((res) => {
					if ( res.data.code === '200') {
						this.form.img = res.data.data
					} else {
						ElMessage.error("图片上传失败")
					}
				});
		},

		load() {

			this.$api.post("/food/page", {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				searchPARAM: this.searchPARAM
			}).then((res) => {
				this.tableData = res.data.data.records;
				this.total = res.data.data.total;
				this.loading = false;
			});


		},

		reset() {
			this.searchPARAM = {
				id: '',
				name: '',
				type: '',
				content: '',
				restaurantName: '',
				restaurantAddress: '',
				img: ''
			}
			this.load();
		},


		add() {
			this.dialogVisible = true;
			this.judge = false;
		},

		cancel() {
			this.$refs.form.resetFields();
			this.dialogVisible = false;
			this.fromImg = '';
		},

		save() {
			
			if(this.judge === false){
					this.$api.post("/food/add",this.form)
					.then((res) => {
						if(res.data.code === '200'){
							ElMessage({message: "添加成功",type: 'success',})
							
						} else{
							ElMessage.error(res.data.msg)
						}
						this.$refs.form.resetFields();
						this.dialogVisible = false;
						this.load();
					});
			}else {
				this.$api.put("/food/update",this.form)
				.then((res) => {
					if(res.data.code === '200'){
						ElMessage({message: "修改成功",type: 'success',})
						
					} else{
						ElMessage.error(res.data.msg)
					}
					this.$refs.form.resetFields();
					this.dialogVisible = false;
					this.load();
				});
			}
			
			
			
			
			
		},


		handleSizeChange(pageSize) {
			//改变每页个数
			this.pageSize = pageSize;
			this.load();
		},
		handleCurrentChange(pageNum) {
			//改变页码
			this.currentPage = pageNum;
			this.load();
		},

		async handleDelete(id) {
			//删除

			this.$api.delete("/food/delete/" + id).then((res) => {
				if (res.data.code === "200") {
					ElMessage({
						message: "删除成功",
						type: "success",
					});
					this.load();
				} else {
					ElMessage.error(res.data.msg)
				}
			});
		},

		handleEdit(row) {
			//修改
			//判断操作类型
			this.judge = true;
			this.dialogVisible = true;
			this.$nextTick(() => {
				this.$refs.form.resetFields();
				this.form = JSON.parse(JSON.stringify(row));
				this.fromImg = this.imgUrl+row.img;
				console.log(this.form.content)

			});
		},


	},






}
